package com.transsion.clouddemo.core.network

import com.transsion.lib_http.interceptor.DomainSwitchInterceptor
import com.transsion.lib_http.interceptor.HeaderInterceptor
import com.transsion.lib_http.interceptor.SignInterceptor
import com.transsion.lib_http.utilcode.util.Utils
import okhttp3.Cache
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import okhttp3.Response
import java.io.File
import java.io.IOException
import java.util.concurrent.TimeUnit

object CloudOkHttpClient {

        private val mOkHttpClient by lazy {
            val newBuilder = OkHttpClient().newBuilder()
            newBuilder.connectTimeout(60, TimeUnit.SECONDS).readTimeout(60, TimeUnit.SECONDS)
                .writeTimeout(60, TimeUnit.SECONDS).retryOnConnectionFailure(true)
            // 添加拦截器
            newBuilder.addInterceptor(DomainSwitchInterceptor())
            newBuilder.addInterceptor(HeaderInterceptor())
            newBuilder.addInterceptor(SignInterceptor())
            // 处理缓存
            val cache = Cache(File(Utils.getApp().cacheDir, "http_cache"), 50L * 1024L * 1024L)
            newBuilder.cache(cache)
            newBuilder.build()
        }

    fun getOkHttpClient() = mOkHttpClient


}